<template>
  <div class="layout-padding">
    <div class="prod-groud-box">


      <div class="left-box">
        <div class="layout-padding-auto layout-padding-view">
          <el-row v-show="showSearch">
            <el-form :model="state.queryForm" ref="queryRef" :inline="true" @keyup.enter="getDataList">
              <el-form-item prop="name" :label="$t('ProdGroup.name')">
                <el-input v-model="state.queryForm.name" :placeholder="$t('ProdGroup.inputNameTip')" clearable
                  @clear="getDataList" />
              </el-form-item>
              <el-form-item prop="shopId" :label="$t('ProdGroup.shopId')">

                <ShopSelect v-model="state.queryForm.shopId" />
                <!-- <el-input v-model="state.queryForm.shopId" :placeholder="$t('ProdGroup.inputShopIdTip')" clearable
              @clear="getDataList" /> -->
              </el-form-item>
              <el-form-item>
                <el-button icon="search" type="primary" @click="getDataList">
                  {{ $t('common.queryBtn') }}
                </el-button>
                <el-button icon="Refresh" @click="resetQuery">{{ $t('common.resetBtn') }}</el-button>
              </el-form-item>
            </el-form>
          </el-row>
          <el-row>
            <div class="mb8" style="width: 100%">
              <el-button icon="folder-add" type="primary" class="ml10" @click="formDialogRef.openDialog()"
                v-auth="'bizs_ProdGroup_add'">
                {{ $t('common.addBtn') }}
              </el-button>
              <el-button plain :disabled="multiple" icon="Delete" type="primary" v-auth="'bizs_ProdGroup_del'"
                @click="handleDelete(selectObjs)">
                {{ $t('common.delBtn') }}
              </el-button>
              <right-toolbar v-model:showSearch="showSearch" :export="'bizs_ProdGroup_export'" @exportExcel="exportExcel"
                class="ml10 mr20" style="float: right;" @queryTable="getDataList"></right-toolbar>
            </div>
          </el-row>
          <el-table :data="state.dataList" v-loading="state.loading" border :cell-style="tableStyle.cellStyle"
            :header-cell-style="tableStyle.headerCellStyle" @selection-change="selectionChangHandle" highlight-current-row
            @current-change="handleCurrentChange" @sort-change="sortChangeHandle">
            <el-table-column type="selection" width="40" />
            <!--<el-table-column type="index" label="#" width="40" />-->
            <el-table-column prop="name" :label="$t('ProdGroup.name')" show-overflow-tooltip />
            <!-- <el-table-column prop="shopId" :label="$t('ProdGroup.shopId')" show-overflow-tooltip /> -->
            <el-table-column prop="state" :label="$t('ProdGroup.state')" show-overflow-tooltip>
              <template #default="scope">
                <dict-tag :options="common_state_enable" :value="scope.row.state"></dict-tag>
              </template>
            </el-table-column>
            <!-- <el-table-column prop="isDefault" :label="$t('ProdGroup.isDefault')" show-overflow-tooltip /> -->
            <el-table-column prop="prodCount" :label="$t('ProdGroup.prodCount')" show-overflow-tooltip />
            <el-table-column prop="style" :label="$t('ProdGroup.style')" show-overflow-tooltip>
              <template #default="scope">
                <dict-tag :options="biz_prod_group_style" :value="scope.row.style"></dict-tag>
              </template>
            </el-table-column>
            <el-table-column prop="sort" :label="$t('ProdGroup.sort')" show-overflow-tooltip />
            <el-table-column :label="$t('common.action')" width="150">
              <template #default="scope">
                <el-button icon="edit-pen" text type="primary" v-auth="'bizs_ProdGroup_edit'"
                  @click="formDialogRef.openDialog(scope.row.id)">{{ $t('common.editBtn') }}</el-button>
                <el-button icon="delete" text type="primary" v-auth="'bizs_ProdGroup_del'"
                  @click="handleDelete([scope.row.id])">{{ $t('common.delBtn') }}</el-button>
              </template>
            </el-table-column>
          </el-table>
          <pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" v-bind="state.pagination" />
        </div>
      </div>
      <ProdGroupReference ref="ProdGroupReferenceREF"></ProdGroupReference>
    </div>
    <!-- 编辑、新增  -->
    <form-dialog ref="formDialogRef" @refresh="getDataList(false)" />

  </div>
</template>

<script setup lang="ts" name="systemProdGroup">
import { BasicTableProps, useTable } from "/@/hooks/table";
import { useMessage, useMessageBox } from "/@/hooks/message";
import { getPage, delObjs } from "/@/api/bizs/ProdGroup";

// 定义i18n
import { useI18n } from 'vue-i18n';
const { t } = useI18n();

// 引入组件
const FormDialog = defineAsyncComponent(() => import('./form.vue'));
const ShopSelect = defineAsyncComponent(() => import("/@/components/ShopSelect/index.vue"))
const ProdGroupReference = defineAsyncComponent(() => import("./components/ProdGroupReference/index.vue"))
// 定义查询字典
import { useDict } from '/@/hooks/dict';

const { common_state_enable, biz_prod_group_style } = useDict('common_state_enable', 'biz_prod_group_style')
// 定义变量内容
const formDialogRef = ref()
const ProdGroupReferenceREF = ref()
// 搜索变量
const queryRef = ref()
const showSearch = ref(true)
// 多选变量
const selectObjs = ref([]) as any
const multiple = ref(true)

const state: BasicTableProps = reactive<BasicTableProps>({
  queryForm: {},
  pageList: getPage
})

//  table hook
const {
  getDataList,
  currentChangeHandle,
  sizeChangeHandle,
  sortChangeHandle,
  downBlobFile,
  tableStyle
} = useTable(state)

// 清空搜索条件
const resetQuery = () => {
  // 清空搜索条件
  queryRef.value?.resetFields()
  // 清空多选
  selectObjs.value = []
  getDataList()
}

// 导出excel
const exportExcel = () => {
  downBlobFile('/bizs/prodGroup/export', Object.assign(state.queryForm, { ids: selectObjs }), 'ProdGroup.xlsx')
}

// 多选事件
const selectionChangHandle = (objs: { id: string }[]) => {
  selectObjs.value = objs.map(({ id }) => id);
  multiple.value = !objs.length;
};

const currentTabelItem = ref()

provide("currentTabelItem", currentTabelItem)

// 当前表格 current 事件
const handleCurrentChange = (e: any) => {
  currentTabelItem.value = e
  ProdGroupReferenceREF.value.state.queryForm.groupId = e.id
  ProdGroupReferenceREF.value.getDataList()
}


// 删除操作
const handleDelete = async (ids: string[]) => {
  try {
    await useMessageBox().confirm(t('common.delConfirmText'));
  } catch {
    return;
  }

  try {
    await delObjs(ids);
    getDataList();
    useMessage().success(t('common.delSuccessText'));
  } catch (err: any) {
    useMessage().error(err.msg);
  }
};
</script>

<style lang="scss" scoped>
.prod-groud-box {
  display: flex;
  height: 100%;
  gap: 10px;
  width: 100%;

  .left-box {
    width: 36%;
  }
}
</style>
